package com.aizhixin.lab.course.course.service;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.core.PageData;
import com.aizhixin.lab.common.core.PageUtil;
import com.aizhixin.lab.common.domain.PageDomain;
import com.aizhixin.lab.company.entity.User;
import com.aizhixin.lab.company.service.UserService;
import com.aizhixin.lab.course.course.domain.*;
import com.aizhixin.lab.course.course.entity.Course;
import com.aizhixin.lab.course.course.entity.CourseLike;
import com.aizhixin.lab.course.course.entity.TeacherLike;
import com.aizhixin.lab.course.course.repository.CourseJdbc;
import com.aizhixin.lab.course.course.repository.CourseLikeRepository;
import com.aizhixin.lab.course.course.repository.CourseRepository;
import com.aizhixin.lab.course.course.repository.TeacherLikeRepository;
import com.aizhixin.lab.statistics.entity.FileLookData;
import com.aizhixin.lab.statistics.repository.FileLookDataRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@Transactional
public class StudentCourseService {
    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private CourseJdbc courseJdbc;
    @Autowired
    private UserService userService;
    @Autowired
    private FileLookDataRepository dataRepository;
    @Autowired
    private CourseLikeRepository courseLikeRepository;
    @Autowired
    private TeacherLikeRepository teacherLikeRepository;
    public PageData<CourseStuListDomain> getList(Long stuId, String name, Integer pageNumber, Integer pageSize) {
        Pageable pageable = PageUtil.createRequestAndSortType(pageNumber, pageSize, "desc", "id");
        List<CourseStuListDomain> list = courseJdbc.getStudentCourseList(pageable, stuId, name);
        Long count = courseJdbc.countStudentCourseList(stuId, name);
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageSize(pageable.getPageSize());
        pageDomain.setPageNumber(pageable.getPageNumber());
        pageDomain.setTotalElements(count);
        BigDecimal obj = new BigDecimal(count);
        int totalPage = (int) Math.ceil(obj.divide(new BigDecimal(pageable.getPageSize())).doubleValue());
        pageDomain.setTotalPages(totalPage);
        PageData<CourseStuListDomain> pageData = new PageData<>();
        pageData.setData(list);
        pageData.setPage(pageDomain);
        return pageData;
    }
    public void look(Long fileId, AccountDTO accountDTO){
        FileLookData fileLookData = new FileLookData();
        fileLookData.setFileId(fileId);
        fileLookData.setStuId(accountDTO.getId());
        fileLookData.setClassId(accountDTO.getClassId());
        dataRepository.save(fileLookData);
    }
    public CourseStuDomain getCourse(String courseId, Long stuId) {
        CourseStuDomain domain = new CourseStuDomain();
        Course course = courseRepository.findByIdAndIsPublishAndIsArchiveAndDeleteFlag(courseId, true, false, DataValidity.VALID.getState());
        if (course != null) {
            BeanUtils.copyProperties(course, domain);
            //授课教师
            User teacher = userService.getUser(null, course.getCreatedBy());
            domain.setTeacherName(teacher.getName());
            domain.setTeacherAvatar(teacher.getAvatar());
            domain.setTeacherId(teacher.getId());
            domain.setCountChapter(course.getViewCountChapter());
            domain.setCountSection(course.getViewCountSection());
            TeacherLike teacherLike = teacherLikeRepository.findByCreatedByAndTeacherIdAndCourseId(stuId, teacher.getId(), courseId);
            if (!Objects.isNull(teacherLike)){
                domain.setTeacherLike(true);
            }
            CourseLike courseLike = courseLikeRepository.findByCourseIdAndCreatedBy(courseId, stuId);
            if (!Objects.isNull(courseLike)){
                domain.setCourseLike(true);
            }
            //同班同学
            List<CourseStuInfoDomain> list = courseJdbc.getCourseStuInfoList(course.getId(), stuId);
            List<CourseStuInfoDomain> stuList = new ArrayList<>();
            for (CourseStuInfoDomain item : list) {
                if (item.getName() != null) {
                    stuList.add(item);
                }
            }
            domain.setStuInfoDomainList(stuList);
        }
        return domain;
    }
}
